package com.wyl.niupi.yxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.niupi.framework.constant.Const;
import com.wyl.niupi.framework.util._BeanUtil;
import com.wyl.niupi.framework.vo.PageResultVo;
import com.wyl.niupi.framework.vo.ResponseVo;
import com.wyl.niupi.framework.vo.Vo;
import com.wyl.niupi.frameworkExt.controller.BaseControllerExt;
import com.wyl.niupi.yxc.entity.YxcGoodsType;
import com.wyl.niupi.yxc.service.YxcGoodsTypeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


/**
 * @author 无名氏 on 2020/12/04 10:06
 */
@Tag(name = "YxcGoodsTypeController", description = "测试相关接口")
@RestController
@RequestMapping("/yxc/yxcGoodsType")
public class YxcGoodsTypeController extends BaseControllerExt {

    @Autowired
    private YxcGoodsTypeService yxcGoodsTypeService;

    @Operation(summary = "获取列表", parameters = {
        @Parameter(name = "where_字段名称_过滤规则", example = "where_cname_like", required = false),
        @Parameter(name = "page", example = "1", description = "如果page和row都不为空则进行分页", required = false),
        @Parameter(name = "rows", example = "15", description = "如果page和row都不为空则进行分页", required = false),
    }, description = "详细描述...")
    @GetMapping("/list")
    public ResponseVo<PageResultVo<List<YxcGoodsType>>> list(HttpServletRequest request){
        QueryWrapper queryWrapper = getQueryWrapper(request);
        queryWrapper.eq(YxcGoodsType.IS_DELETE, Const.IS_NO);

        IPage<YxcGoodsType> ipage = getPage(request);
        if (null != ipage) {
            ipage = yxcGoodsTypeService.selectPage(ipage, queryWrapper);
            List<Vo<YxcGoodsType>> list = Vo.list(ipage.getRecords());
            return sendSuccess(list);
        } else {
            List list = yxcGoodsTypeService.list(queryWrapper);
            return sendSuccess(list);
        }
    }

    @Operation(summary = "根据id获取", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = true),
    })
    @GetMapping("/get")
    public ResponseVo<YxcGoodsType> get(@RequestParam("lid") Integer lid) {
        YxcGoodsType bo = yxcGoodsTypeService.getById(lid);
        return sendSuccess(bo);
    }

    @Operation(summary = "添加")
    @PostMapping("/add")
    public ResponseVo add(YxcGoodsType bo) {
        bo.setIsDelete(Const.IS_NO);
        boolean b = yxcGoodsTypeService.save(bo);
        return sendSuccess(b);
    }

    @Operation(summary = "根据id删除", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = true),
    })
    @PostMapping("/delete")
    public ResponseVo delete(@RequestParam("lid") Integer lid) {
        YxcGoodsType bo = yxcGoodsTypeService.getById(lid);
        bo.setIsDelete(Const.IS_YEW);
        boolean b = yxcGoodsTypeService.updateById(bo);
        return sendSuccess(b);
    }

    @Operation(summary = "根据id修改", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = true),
        @Parameter(name = "其他属性", description = "传什么属性就修改什么，不传的属性不修改", required = false),
    })
    @PostMapping("/update")
    public ResponseVo update(@RequestParam("lid") Integer lid, HttpServletRequest request) {
        try {
            YxcGoodsType oldBo = yxcGoodsTypeService.getById(lid);
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            boolean b = yxcGoodsTypeService.saveOrUpdate(oldBo);
            return sendSuccess(b);
        }catch (IllegalAccessException | InvocationTargetException e) {
            return sendError(e.getMessage());
        }
    }


}

